/*
 * LoginFrame.java
 *
 * Created on 2011-12-11, 13:39:44
 */
package iobankapi;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import userfunctions.LoginManager;
import userfunctions.Person;


/**
 * Klasa okienka logowania
 * @author Kamil Sobala
 */
public class LoginFrame extends javax.swing.JFrame {

    /**
     * Domyślny konstruktor klasy
     */
    public LoginFrame() {
        super();
        mainPanel = new JPanel();
        
        
        BoxLayout mainLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
        
        mainPanel.setLayout(mainLayout);
        
        add(mainPanel);
        ImageIcon logoIcon = new ImageIcon("Logo.png");
        JLabel logo = new JLabel(logoIcon);
        mainPanel.add(logo);
        mainPanel.setBackground(Color.white);
        
        mainPanel.add(Box.createVerticalGlue());
        
        centerPanel1 = new JPanel();
        loginLabel = new JLabel("Login: ");
        centerPanel1.add(loginLabel);
        loginField = new JTextField(20);
        centerPanel1.add(loginField);
        centerPanel1.setBackground(Color.WHITE);
        mainPanel.add(centerPanel1);
        
        centerPanel2 = new JPanel();
        passLabel = new JLabel("Hasło:");
        centerPanel2.add(passLabel);
        passField = new JPasswordField(20);
        centerPanel2.add(passField);
        centerPanel2.setBackground(Color.white);
        mainPanel.add(centerPanel2);
        
        centerPanel3 = new JPanel();
        LogInBtn = new JButton("Zaloguj");
        LogInBtn.addActionListener(new LogInListener());
        centerPanel3.add(LogInBtn);
        centerPanel3.setBackground(Color.WHITE);
        
        mainPanel.add(centerPanel3);
        
        
        
        setSize(480,400);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
    }


private JPanel mainPanel;
private JPanel centerPanel1;
private JLabel loginLabel;
private JTextField loginField;
private JLabel passLabel;
private JPasswordField passField;
private JPanel centerPanel2;
private JPanel centerPanel3;
private JButton LogInBtn;

/**
 * Prywatna klasa implementująca inteferjs ActionListener
 * Deklaracja metody actionPerformed dla klikniętego przycisku Zaloguj
 */
 
private class LogInListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {
           LoginManager loginManager = new LoginManager();
           Person person = loginManager.loginUser(loginField.getText(), new String(passField.getPassword()));
           if(person == null){
               JOptionPane.showMessageDialog(LoginFrame.this, "Sprawdź login i hasło i spróbuj ponownie.","Logowanie nieudane!", JOptionPane.ERROR_MESSAGE);
           }
           else{
            MainFrame mainFrame = new MainFrame(person);
            mainFrame.setVisible(true);
            LoginFrame.this.dispose();
           }
           
        }
    }

    

}
